import { getSalesData, getPvData, getPaymentData, getUserActiveData, getOrderChartData  } from '../services/api';

export default {
  namespace: 'dashboard',

  state: {
    loading: false,
    salesData : {},
    pvData : {
      pvDetail : []
    },
    paymentData : {
      detail : []
    },
    userActive : {},
    orderData : [],
  },

  effects: {
    *fetchSalesData(_, { call, put }) {
      const response = yield call(getSalesData);
      if(response){
        yield put({
          type: 'save',
          payload: {
            salesData: response,
          },
        });
      }
    },
    *fetchPvData({ payload }, { call, put }) {
      const response = yield call(getPvData, payload);
      if(response){
        yield put({
          type: 'save',
          payload: {
            pvData: response,
          },
        });
      }
    },
    *fetchPaymentData(_, { call, put }) {
      const response = yield call(getPaymentData);
      if(response){
        yield put({
          type: 'save',
          payload: {
            paymentData: response,
          },
        });
      }
    },
    *fetchUserActiveData(_, { call, put }) {
      const response = yield call(getUserActiveData);
      if(response){
        yield put({
          type: 'save',
          payload: {
            userActive: response,
          },
        });
      }
    },
    *fetchOrderData({ payload }, { call, put }) {
      const response = yield call(getOrderChartData, payload);
      if(response){
        yield put({
          type: 'save',
          payload: {
            orderData : response
          },
        });
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    clear() {
      return {
        salesData : {},
        pvData : {
          pvDetail : []
        },
        paymentData : {
          detail : []
        },
        userActive : {},
        orderData : [],
      };
    },
  },
};
